//
//  TWNetworkTool.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/10.
//  Copyright © 2017年 个人. All rights reserved.
//

import AFNetworking

/// 请求方式
enum TWRequestType {
    case get
    case post
}


// MARK:- 单例
class TWNetworkTool: AFHTTPSessionManager {
    static let shareInstance : TWNetworkTool = {
        let toolInstance = TWNetworkTool()
        // 添加网络形式
        toolInstance.responseSerializer.acceptableContentTypes?.insert("text/html")
        toolInstance.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return toolInstance
    }()
}


// MARK:- 封装请求方法
extension TWNetworkTool {
    // get或者post请求
    func request(methodType : TWRequestType, URLString : String,parameters : [String : AnyObject], resultBlock : @escaping (_ result : AnyObject?, _ error : Error?)->()) {
        
        // 成功的闭包
        let successBlock = { (task: URLSessionDataTask, responseObj: Any?) in
            resultBlock(responseObj as AnyObject?, nil)
        }
        // 失败的闭包
        let failureBlock = { (task : URLSessionDataTask?, error : Error) in
            resultBlock(nil, error)
        }
        
        // get请求
        if methodType == .get {
            get(URLString, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
        
        // post请求
        if methodType == .post {
            post(URLString, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
    }
}


// MARK:- 首页的数据请求
extension TWNetworkTool {
    func loadChoicenessData(urlString : String, finished: @escaping ( _ result : [String : AnyObject]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict, error)
        }
    }
}

// MARK:- 关注页的数据请求
extension TWNetworkTool {
    func loadSubscriptionData(urlString : String, finished: @escaping ( _ result : [String : AnyObject]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict, error)
        }
    }
}

// MARK:- 作者页的数据请求
extension TWNetworkTool {
    func loadAuthorData(urlString : String, finished: @escaping ( _ result : [String : AnyObject]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict, error)
        }
    }
}


// MARK:- 相关页请求
extension TWNetworkTool {
    func loadCorrelationData(urlString : String, finished: @escaping ( _ result : [[String : AnyObject]]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict["itemList"] as! [[String : AnyObject]]?, error)
        }
    }
}

// MARK:- 发现页的标题请求
extension TWNetworkTool {
    func loadTitleViewData(finished: @escaping ( _ result : [[String : AnyObject]]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: DiscoverUrlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict["tabInfo"]?["tabList"] as! [[String : AnyObject]]?, error)
        }
    }
}

// MARK:- 发现页的内容请求
extension TWNetworkTool {
    func loadDiscoverViewData(urlString : String, finished: @escaping ( _ result : [String : AnyObject]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict, error)
        }
    }
}

// MARK:- 全部分类的请求
extension TWNetworkTool {
    func loadAllCategoriesData(urlString : String, finished: @escaping ( _ result : [[String : AnyObject]]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict["itemList"] as! [[String : AnyObject]]?, error)
        }
    }
}

// MARK:- 作者页的标题请求
extension TWNetworkTool {
    func loadAuthorTitleViewData(urlString : String, finished: @escaping ( _ result : [String : AnyObject]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let resultDict = result as? [String : AnyObject] else {
                return
            }
            // 将字典回调出去
            finished(resultDict as [String : AnyObject]?, error)
        }
    }
}

// MARK:- 搜索页的热词
extension TWNetworkTool {
    func loadHotWordsData(urlString : String, finished: @escaping ( _ result : [String]?, _ error : Error?)->()) {
        request(methodType: .get, URLString: urlString, parameters: [:]) { (result, error) in
            // 获取字典，进行校验
            guard let result = result as? [String] else {
                return
            }
            // 将字典回调出去
            finished(result as [String]?, error)
        }
    }
}










